﻿Public Class Form1

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim frm As frmFlash = New frmFlash()
        frm.Show()
        System.Threading.Thread.Sleep(3000)
        frm.Close()

    End Sub

    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MacDinh()

        Dim frmDN As frmDangNhap = New frmDangNhap()
        frmDN.ShowDialog()

    End Sub


    Private Sub btnTiepNhanChuNha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTiepNhanChuNha.Click
        Dim frm As frmTiepNhanChuNha = New frmTiepNhanChuNha()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub btnTiepNhanKhach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTiepNhanKhach.Click
        Dim frm As frmTiepNhanKhachThue = New frmTiepNhanKhachThue()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub btnTiepNhanNha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTiepNhanNha.Click
        Dim frm As frmTiepNhanNha = New frmTiepNhanNha()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub btnGhiNhanThueNha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGhiNhanThueNha.Click
        Dim frm As frmGhiNhanThueNha = New frmGhiNhanThueNha()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub btnTraCuuNha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTraCuuNha.Click
        Dim frm As frmTraCuuNha = New frmTraCuuNha()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub btnDuLieuTamXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDuLieuTamXoa.Click
        Dim frmDuLieuTamXoa As frmQLDuLieuTamXoa = New frmQLDuLieuTamXoa()
        frmDuLieuTamXoa.ShowDialog()
    End Sub

    Private Sub btnLapBaocaoThang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLapBaocaoThang.Click
        Dim frm As frmLapBaoCaoThang = New frmLapBaoCaoThang()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        If MessageBox.Show("Bạn có muốn thoát chương trình ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            Me.Close()
        End If
    End Sub

    Private Sub btnSaoLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaoLuu.Click
        backupDialog.Filter = "Sao Lưu File (*.bak)|*.bak"

        If backupDialog.ShowDialog() = DialogResult.OK Then
            'Thuc hien lenh Backup Database
            Dim DuongDan As String = backupDialog.FileName
            Dim DuLieu As String = "QL_GioiThieuNhaThue"

            Backup_RestoreDuLieuBUS.Backup(DuLieu, DuongDan)

            MessageBox.Show("Sao lưu dữ liệu thành công!!!")
        End If
    End Sub

    Private Sub btnPhucHoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPhucHoi.Click
        restoreDialog.Filter = "Phục hội từ File (*.bak)|*.bak"
        If restoreDialog.ShowDialog() = DialogResult.OK Then
            'Thuc hien lenh Restore Database
            Dim DuongDan As String = restoreDialog.FileName
            Dim DuLieu As String = "QL_GioiThieuNhaThue"

            Backup_RestoreDuLieuBUS.Restore(DuLieu, DuongDan)

            MessageBox.Show("Phục hồi dữ liệu thành công!!!")
        End If
    End Sub

    Private Sub btnDangXuat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDangXuat.Click
        MacDinh()
        While maintab.Tabs.Count > 0
            maintab.Tabs.RemoveAt(maintab.SelectedTabIndex)
        End While
        maintab.Visible = False

        lbUID.Text = "Bạn cần đăng nhập để sử dụng phần mềm."
    End Sub

    Private Sub btnDangNhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDangNhap.Click
        frmDangNhap.ShowDialog()
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If MessageBox.Show("Bạn có muốn thoát chương trình ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
            e.Cancel = True
        End If
    End Sub

    Private Sub btnQuanLyCN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuanLyCN.Click
        Dim frm As frmQuanLyChuNha = New frmQuanLyChuNha()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub btnQuanLyKH_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuanLyKH.Click
        Dim frm As frmQuanLyKhachHang = New frmQuanLyKhachHang()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub btnQuanLyNha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuanLyNha.Click
        Dim frm As frmQuanLyNha = New frmQuanLyNha()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub maintab_TabItemClose(ByVal sender As System.Object, ByVal e As DevComponents.DotNetBar.TabStripActionEventArgs) Handles maintab.TabItemClose
        maintab.Tabs.RemoveAt(maintab.SelectedTabIndex)
        If maintab.Tabs.Count = 0 Then
            maintab.Visible = False
        End If
    End Sub

    
    Private Sub btnQuanLyQuyDinh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuanLyQuyDinh.Click
        Dim frm As frmQuanLyQuyDinh = New frmQuanLyQuyDinh()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub btnQLTTThueNha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQLTTThueNha.Click
        Dim frm As frmQuanLyThongTinThueNha = New frmQuanLyThongTinThueNha()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub


    Public Sub CreateTab(ByVal frmName As Form, ByVal tabcontrol As DevComponents.DotNetBar.TabControl)
        tabcontrol.Visible = True
        Dim t As DevComponents.DotNetBar.TabItem = maintab.CreateTab(frmName.Text)
        frmName.TopLevel = False
        frmName.Dock = DockStyle.Fill
        frmName.FormBorderStyle = FormBorderStyle.None
        t.AttachedControl.Controls.Add(frmName)
        frmName.Show()
        tabcontrol.SelectedTabIndex = tabcontrol.Tabs.Count - 1
    End Sub
    Public Function CheckOpenTab(ByVal tabName As String, ByVal tabcontrol As DevComponents.DotNetBar.TabControl) As Boolean
        For i As Integer = 0 To tabcontrol.Tabs.Count - 1
            If tabcontrol.Tabs(i).Text = tabName Then
                tabcontrol.SelectedTabIndex = i
                Return True
            End If
        Next
        Return False
    End Function

    Private Sub btnQLPhanQuyen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQLPhanQuyen.Click
        Dim frm As frmQuanLyPhanQuyen = New frmQuanLyPhanQuyen()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Public Sub MacDinh()

        rbTiepNhan.Enabled = False
        rbQuanLy.Enabled = False
        btnTraCuuNha.Enabled = False
        Export.Enabled = False
        Import.Enabled = False
        btnLapBaocaoThang.Enabled = False
        btnLapBaoCaoNha.Enabled = False
        rbDuLieu.Enabled = False
        btnDangXuat.Enabled = False
        btnDangNhap.Enabled = True

    End Sub
    Public Sub QuanLy()
        rbQuanLy.Enabled = True
        Export.Enabled = True
        Import.Enabled = True
        rbDuLieu.Enabled = True
        btnDangNhap.Enabled = False
        btnDangXuat.Enabled = True

        btnQuanLy.Select()

    End Sub
    Public Sub NhanVien()
        rbTiepNhan.Enabled = True
        btnTraCuuNha.Enabled = True
        btnLapBaocaoThang.Enabled = True
        btnLapBaoCaoNha.Enabled = True
        btnDangNhap.Enabled = False
        btnDangXuat.Enabled = True
        
        TiepNhan.Select()
    End Sub

    Private Sub btnExportDSNha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportDSNha.Click
        Dim frm As frmExportDSNha = New frmExportDSNha()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub btnExportDSChuNha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportDSChuNha.Click
        Dim frm As frmExportDS_ChuNha = New frmExportDS_ChuNha()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub btnExportDSKhachThue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportDSKhachThue.Click
        Dim frm As frmExportDSKhachHang = New frmExportDSKhachHang()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub btnImportNha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportNha.Click
        Dim frm As frmImportNha = New frmImportNha()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub btnImportChuNha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportChuNha.Click
        Dim frm As frmImportChuNha = New frmImportChuNha()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub btnImportKhachHang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportKhachHang.Click
        Dim frm As frmImportKhachHang = New frmImportKhachHang()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub btnLapBaoCaoNha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLapBaoCaoNha.Click
        Dim frm As frmBaoCaoNha = New frmBaoCaoNha()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub ButtonItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonItem1.Click
        Dim frm As frmAbout = New frmAbout()
        If Not CheckOpenTab(frm.Text, maintab) Then
            CreateTab(frm, maintab)
        End If
    End Sub

    Private Sub btnTroGiup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTroGiup.Click
        Process.Start("TroGiup_NhaThue.chm")
        'Process.Start("..\..\Help\TroGiup_NhaThue.chm")
    End Sub
End Class
